package com.example.as_sum.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.example.as_sum.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 测试动画 sdasdsdasdasdada
 * 合并成功
 */
public class AnimActivity extends AppCompatActivity {

    @Bind(R.id.anim_view)
    View view_move;
    @Bind(R.id.anim_view2)
    View view_move2;
    //ceshi1o1
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);
        ButterKnife.bind(this);
        int []location=new int[2];
        view_move.getLocationInWindow(location);
        point=new Point(location[0],location[1]);
        logPoint(1);
    }
    private void logPoint(int type){
        if(type==1)
            Log.e("===初始坐标===>", "获取view的当前坐标 x:"+point.x+",y:"+point.y);
        else if(type==2)
            Log.e("===动画开始===>", "获取view的当前坐标 x:"+point.x+",y:"+point.y);
    }
    private Point point;
    private ValueAnimator animator;
    ValueAnimator valueAnimator;
    public void animBtn1() {
        animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(2000);
        animator.setRepeatCount(1);//0表示不重复，设置下面的重复模式就没效果，
        //设置成大于0或者ValueAnimator.INFINITE下面的重复方式就起作用了
        //如果设置成ValueAnimator.REVERSE的话，第一次从头到尾，第二次从尾到头
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x=(float)animation.getAnimatedValue();
                view_move.setX(x*200);
                logPoint(2);
            }
        });
        animator.start();
    }

    @OnClick({R.id.anim_btn1, R.id.anim_btn2, R.id.anim_btn3, R.id.anim_btn4, R.id.anim_btn5, R.id.anim_btn6, R.id.anim_btn7, R.id.anim_btn8, R.id.anim_btn9, R.id.anim_btn10})
    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.anim_btn1:
                animBtn1();
                break;
            case R.id.anim_btn2:
                ValueAnimator animator=ValueAnimator.ofInt(0,300);
                animator.setRepeatCount(0);
                animator.setDuration(3000);
                animator.setRepeatMode(ValueAnimator.REVERSE);
                animator.setInterpolator(new BounceInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int y=(int)animation.getAnimatedValue();
                        view_move.setTranslationY(y);
                    }
                });
                animator.start();
                break;
            case R.id.anim_btn3:
                ValueAnimator animator2=ValueAnimator.ofInt(0,300);
                animator2.setRepeatCount(1);
                animator2.setDuration(5000);
                animator2.setRepeatMode(ValueAnimator.REVERSE);
                animator2.setInterpolator(new MyTimeInterpolator());
                animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int y=(int)animation.getAnimatedValue();
                        view_move.setTranslationX(y);
                        view_move.setTranslationY(y);
                    }
                });
                animator2.start();
                break;
            case R.id.anim_btn4:
                Point startPoint,endPoint,centerPoint;
                startPoint=point;
                endPoint=new Point(500,500);
                final int pointX = (startPoint.x + endPoint.x) / 2;
                //int pointY = startPoint.y/5;
                int pointY = startPoint.y;
                centerPoint=new Point(pointX,pointY);
                valueAnimator=ValueAnimator.ofObject(new MyTypeEvaluator(centerPoint),startPoint,endPoint);
                valueAnimator.setDuration(5000);
                valueAnimator.setInterpolator(new LinearInterpolator());
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        Point point=(Point)animation.getAnimatedValue();
                        view_move.setX(point.x);
                        view_move.setY(point.y);
                    }
                });
                valueAnimator.setRepeatCount(0);
                valueAnimator.start();
                break;
            case R.id.anim_btn5:
                if(valueAnimator.isRunning())
                    valueAnimator.cancel();
                break;
            case R.id.anim_btn6:
                if(valueAnimator.isRunning())
                    valueAnimator.end();
                break;
            case R.id.anim_btn7:
                valueAnimator.reverse();
                break;
            case R.id.anim_btn8:
                AnimatorSet animatorSet=new AnimatorSet();
                ValueAnimator animator1=new ValueAnimator();
                animator1.setFloatValues(0,1);
                animator1.setDuration(3000);
                animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float f=(float)animation.getAnimatedValue();
                        view_move2.setAlpha(1-f);
                    }
                });

                ValueAnimator animator3=ValueAnimator.ofFloat(0,1);
                animator3.setDuration(3000);
                animator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float f=(float)animation.getAnimatedValue();
                        view_move2.setTranslationX(f*400);
                        view_move2.setTranslationY(f*400);
                    }
                });
                animatorSet.play(animator1).after(animator3);//先执行animator3在执行animator1
//                animatorSet.play(animator3).after(2000);//延迟2秒执行animator3动画
//                animatorSet.play(animator1).with(animator3);//同时运行两个动画
//                animatorSet.play(animator1).before(animator3);//先执行animator1在执行animator3
//                animatorSet.playTogether(animator1,animator3);//一起执行动画
//                animatorSet.playSequentially();//顺序执行动画
                animatorSet.start();
                break;
            case R.id.anim_btn9:
                break;
            case R.id.anim_btn10:
                break;
        }
    }
    //自定义TimeInterpolator，实现先减速后加速的效果
    class MyTimeInterpolator implements TimeInterpolator{

        @Override
        public float getInterpolation(float input) {
            float result;
            if(input<0.5){
                result=(float)(Math.sin(Math.PI*input))/2;
            }else{
                result=(float)(2-Math.sin(Math.PI*input))/2;
            }
            return result;
        }
    }
    //通过动画之间的插值来提供下一个对象，就是动画过程中的点是通过这个evaluate计算所得的
    class MyTypeEvaluator implements TypeEvaluator<Point> {
        private Point controllPoint;

        public MyTypeEvaluator(Point controllPoint) {
            this.controllPoint = controllPoint;
        }

        @Override
        public Point evaluate(float t, Point startValue, Point endValue) {
            int x = (int) ((1 - t) * (1 - t) * startValue.x + 2 * t * (1 - t) * controllPoint.x + t * t * endValue.x);
            int y = (int) ((1 - t) * (1 - t) * startValue.y + 2 * t * (1 - t) * controllPoint.y + t * t * endValue.y);
            return new Point(x, y);
        }
    }
    private void initAnim() {

        //animator=ValueAnimator.ofArgb(1,255);//api=21才能使用，控制透明度
        animator = ValueAnimator.ofFloat(1, 22);//设置从1--22的变化
        // animator=ValueAnimator.ofInt(1,22);//设置从1--22变化
        //这个是设置两个Object来设置动画开始与结束的效果的，第一参数是动画过程控制动画效果
        // animator=ValueAnimator.ofObject(new MyTypeEvaluator(),new Object(),new Object());
        animator.setDuration(2000);
        animator.setStartDelay(1000);//延迟开始动画
//        animator.setCurrentFraction(200);//api=22的时候才能调用
        animator.setCurrentPlayTime(2000);//

        //ValueAnimator.INFINITE,//Animation.INFINITE 表示重复多次
        // ValueAnimator.RESTART,//RESTART表示从头开始
        // ValueAnimator.REVERSE//REVERSE表示从末尾倒播
        animator.setRepeatCount(ValueAnimator.INFINITE);//动画重复次数，默认是0不重复
        animator.setRepeatMode(ValueAnimator.RESTART);//动画结束之后做什么,从头到尾还是尾到头

        // animator.resume();//api=19的时候才可以调用，开始动画
        //animator.pause();//api=19的时候才可以调用，暂停动画
        animator.isRunning();//是否正在运行
        animator.isStarted();//是否开始了
        animator.end();//结束动画
        animator.reverse();//反转动画
        animator.start();//开始
        animator.cancel();//取消动画，Only cancel if the animation is actually running or has been started and is about to run
//        animator.setupEndValues();
//        animator.setupStartValues();

        //api19才可以调用
//        animator.addPauseListener(new Animator.AnimatorPauseListener() {
//            @Override
//            public void onAnimationPause(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationResume(Animator animation) {
//
//            }
//        });
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //获取每次动画变化的float或者int值，用于更新
                animation.getAnimatedValue();
                //invalidata();//用于控件的更新
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

    }


}
